//
//  MyAnnotationView.swift
//  Runner
//
//  Created by James Karl on 2020/5/13.
//  Copyright © 2020 The Chromium Authors. All rights reserved.
//

import Foundation
import MAMapKit
import amap
import UIKit

class MyAnnotationView : MAAnnotationView {
    let mapView: MAMapView
    let registrar: FlutterPluginRegistrar
    let options: AnnotationViewData
    
    init(frame: CGRect, mapView: MAMapView, options: AnnotationViewData, registrar: FlutterPluginRegistrar) {
        self.mapView = mapView;
        self.registrar = registrar;
        self.options = options;
        super.init(frame: frame)
        
        //self.backgroundColor = UIColor.black.withAlphaComponent(0.5)
        
        let textViewSize = 20.0
        let imageSize = 40.0
        
        let parentWidth = Double(frame.width)
        
        let leftText = RRectTextView(
            text: "5",
            frame: CGRect(x: parentWidth / 2 - textViewSize - 6, y: -textViewSize, width: textViewSize, height: textViewSize),
            fillColor: UIColor.purple.withAlphaComponent(0.4),
            borderRadius: textViewSize / 2,
            textSize: 10
        )
        self.addSubview(leftText)
        
        let rightText = RRectTextView(
            text: "9",
            frame: CGRect(x: parentWidth / 2 + 6, y: -textViewSize, width: textViewSize, height: textViewSize),
            fillColor: UIColor.green.withAlphaComponent(0.4),
            borderRadius: textViewSize / 2,
            textSize: 10
        )
        self.addSubview(rightText)
        
        if let icon = options.icon {
            let iconName = registrar.lookupKey(forAsset: icon)
            let image = UIImage(named: iconName)
            let imageView = UIImageView(image: image)
            imageView.frame = CGRect(x: (Double(frame.width) - imageSize)/2, y: Double(frame.height/2) - imageSize, width: imageSize, height: imageSize)
            self.addSubview(imageView)
        }
       
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}
